import Vue from 'vue'
import Router from 'vue-router'
import VueRouter from 'vue-router'
import Home from '@/components/Home'
import Category from '@/components/product/Category'
import Show from '@/components/product/Show'
import List from '@/components/product/List'
import Search from '@/components/product/Search'
import Cart from '@/components/Cart'
import Login from '@/components/auth/Login'
import Check_out from '@/components/order/Check_out'
import Show_pay from '@/components/order/Show_pay'
import Address_list from '@/components/address/Address_list'
import Address_create from '@/components/address/Address_create'
// import Nocart from '@/components/Nocart'


Vue.use(Router)
Vue.use(VueRouter)

const routes = [
   {
      path: '/',
      name: 'Home',
      component: Home
   },
   {
      path: '/product/category',
      name: 'Category',
      component: Category
   },
   {
      path: '/product/show/:id',
      name: 'Show',
      component: Show
   },
   {
      path: '/product/product_list',
      name: 'List',
      component: List
   },
   {
      path: '/product/search',
      name: 'Search',
      component: Search
   },
   {
      path: '/cart',
      name: 'Cart',
      component: Cart,
      meta: {
         requireAuth: true,
      },
   },
   {
      path: '/login',
      name: 'Login',
      component: Login
   },
   // {
   //   path: '/nocart',
   //   name: 'Nocart',
   //   component: Nocart
   // },
   {
      path: '/check_out',
      name: 'Check_out',
      component: Check_out,
      meta: {
         requireAuth: true,
      }
   },
   {
      path: '/address_list',
      name: 'Address_list',
      component: Address_list,
      meta: {
         requireAuth: true,
      }
   },
   {
      path: '/address_create',
      name: 'Address_create',
      component: Address_create,
      meta: {
         requireAuth: true,
      }
   },
   {
      path: '/show_pay',
      name: 'Show_pay',
      component: Show_pay,
      meta: {
         requireAuth: true,
      }
   },
];

const router = new VueRouter({
   routes
});

//匹配路由有则返回ture
router.beforeEach((to, from, next) => {
   if (to.matched.some(r => r.meta.requireAuth)) {
      if (localStorage.token) {
         next();
      } else {
         next({
            path: '/login',
            query: {redirect: to.fullPath}
         })
      }
   } else {
      next();
   }
});

export default router;

